
public class Dungeon {
	private int width = 5;
	private int height = 5;
	private int roomWidth = 0;
	private int roomHeight = 0;
	private int roomOffsetX = 0;
	private int roomOffsetY = 0;
	private Room[][] rooms;
	
	public Dungeon() {
		
		rooms = new Room[width][height];
		
		//creates rooms
		for(int i = 0; i < width; i++) {
			for(int j = 0; j < height; j++) {
				rooms[i][j] = new Room(i, j);
				for(int a = 0; a < 8; a++) {
					roomWidth = (int)(Math.random()*8);
					if(roomWidth < 5)  {
						roomHeight = (int)(Math.random()*8); }
					else {
						roomHeight = (int)(Math.random()*5); }
					rooms[i][j].createRoom(roomWidth, roomHeight);
				}
			}
		}
		//creates hallways
		for(int i = 0; i < width; i++) {
			for(int j = 0; j < height; j++) {
				while (rooms[i][j].getIsConnected() < 1) {
					//North
					if(rooms[i][j].isHallN() == false && this.checkRoomNorth(i, j) == true) {
						if (rooms[i][j-1].isHallS() == true) {
							rooms[i][j].createHallwayNorth();
						}
						else if (Math.random() < .4) {
							rooms[i][j].createHallwayNorth();
							rooms[i][j-1].createHallwaySouth();
						}
					}

					//West
					if(rooms[i][j].isHallW() == false && this.checkRoomWest(i, j) == true) {
						if (rooms[i-1][j].isHallE() == true) {
							rooms[i][j].createHallwayWest();
						}
						else if (Math.random() < .4) {
							rooms[i][j].createHallwayWest();
							rooms[i-1][j].createHallwayEast();
						}
					}

					//South
					if(rooms[i][j].isHallS() == false && this.checkRoomSouth(i, j) == true) {
						if (rooms[i][j+1].isHallN() == true) {
							rooms[i][j].createHallwaySouth();
						}
						else if (Math.random() < .4) {
							rooms[i][j].createHallwaySouth();
							rooms[i][j+1].createHallwayNorth();
						}
					}
					//East
					if(rooms[i][j].isHallE() == false && this.checkRoomEast(i, j) == true) {
						if (rooms[i+1][j].isHallW() == true) {
							rooms[i][j].createHallwayEast();
						}
						else if (Math.random() < .4) {
							rooms[i][j].createHallwayEast();
							rooms[i+1][j].createHallwayWest();
						}
					}
				}
			}
		}
		
		//checks if all rooms are connected and if not fixes the problem
	}
	
	public Room[][] getRooms() {
		return rooms;
	}

	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}
	
	private boolean checkRoomNorth(int i, int j) {
		try {
			rooms[i][j-1].getMaxHeight();
		}
		catch(Exception ArrayIndexOutOfBoundsException){
			return false;
		}
		return true;
	}
	
	private boolean checkRoomWest(int i, int j) {
		try {
			rooms[i-1][j].getMaxHeight();
		}
		catch(Exception ArrayIndexOutOfBoundsException){
			return false;
		}
		return true;
	}
	
	private boolean checkRoomSouth(int i, int j) {
		try {
			rooms[i][j+1].getMaxHeight();
		}
		catch(Exception ArrayIndexOutOfBoundsException){
			return false;
		}
		return true;
	}
	
	private boolean checkRoomEast(int i, int j) {
		try {
			rooms[i+1][j].getMaxHeight();
		}
		catch(Exception ArrayIndexOutOfBoundsException){
			return false;
		}
		return true;
	}
}
